#ifndef __TCPCONNECTION_HPP__
#define __TCPCONNECTION_HPP__
#include "Socket.hpp"
#include "SocketIO.hpp"
#include "InetAddress.hpp"

#include <memory>
#include <sys/socket.h>
#include <functional>

namespace wd
{
class TcpConnection;
class EventLoop;

using std::shared_ptr;
using std::function;

using TcpConnectionPtr = shared_ptr<TcpConnection>;
using CallBack = function<void(TcpConnectionPtr)>;

class TcpConnection:public std::enable_shared_from_this<TcpConnection>
{
public:
    TcpConnection(int);

    void send(const string&);
    string receive();
    bool isClosed()const;
    
    string connectionInfo();
    string clientInfo();
    string peerInfo();

    void shotdown();

    void setCallBack(const CallBack&,const CallBack &,const CallBack&);
    void handleNewConnectionCallBack();
    void handleMessageCallBack();
    void handleOnClosedCallBack();

    void sendToLoop(const string &);

private:
    InetAddress getClientName();
    InetAddress getPeerName();
private:
    Socket _socket;
    SocketIO _socketIO;
    InetAddress _clientAddr;
    InetAddress _peerAddr;

    CallBack _onConnection;
    CallBack _onMessage;
    CallBack _onClosed;

    EventLoop* _loop;
};

}//end of namespace



#endif // __TCPCONNECTION_HPP__

